package com.nancy.ioc.BeanFactoryPostProcessor.importtest;

import com.nancy.ioc.Bean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
       if(importingClassMetadata.hasAnnotation(ImportRegistrarFlag.class.getName())){
           System.out.println("存在ImportRegistrarFlag");
           BeanDefinitionBuilder builder = rootBeanDefinition(Bean.class);
           builder.addConstructorArgValue("ImportBeanDefinitionRegistrar加载bean");
           builder.setRole(BeanDefinition.ROLE_APPLICATION);
           AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
           registry.registerBeanDefinition("importRegistrarFlagBean", beanDefinition);
       }else {
           System.out.println("不存在ImportRegistrarFlag");
       }
    }
}
